import os
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)
    
class GladeBase(object):
	"""This is the Glade Base Window"""
    
	def __init__(self):
		self.glade_name = self.__class__.__name__.lower()
		self.widget_name = self.__class__.__name__

		#Set the Glade file
		self.gladefile = os.path.join(os.path.dirname(__file__), "%s.glade" % (self.glade_name))
		self.glade = gtk.glade.XML(self.gladefile)

	def __startvalidate__(self):
		return True
		
	def __closevalidate__(self):
		return True

	def show_messagedialog(self, title, message, type = gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK):
		dialog = gtk.MessageDialog(
				parent = None,
				flags = gtk.DIALOG_MODAL,
				type = type,
				buttons = buttons,
				message_format = message
			)
		dialog.set_title(title)
		#dialog.connect('response', lambda dialog, response: dialog.destroy())
		r = dialog.run()
		dialog.destroy()
		return r
    
class GladeWindow(GladeBase):
	"""This is the Glade Window"""
        
	def start(self):
		#Get the Main Window, and connect the "destroy" event
		self.app = self.glade.get_widget(self.widget_name)

		if self.app:
			if self.__startvalidate__():
				self.app.show()
				self.app.connect("destroy", self.quit )
				self.app.show_all()

				self.__start__()
				gtk.main()

		else:
			print "no widget in the %s.glade file with id %s" % (self.glade_name, self.widget_name)
	
	def quit(self, *args):
		if self.__closevalidate__():
			self.app.hide()
			gtk.main_quit()


class GladeDialog(GladeBase):
	"""This is the Glade Assistant"""
	
	def show(self, parent = None):
		#Get the Main Assistant, and connect the "close" events
		self.app = self.glade.get_widget(self.widget_name)
		
		if self.app:
			#if parent:
			#	self.app.set_parent(parent)
			self.app.connect('delete_event', self.close)
			self.app.connect('close', self.close)
			
			self.app.show()
			gtk.main()

		else:
			print "no widget in the %s.glade file with id %s" % (self.glade_name, self.widget_name)
	
	def start(self, *args):
		if self.__startvalidate__():
			self.__start__()
			self.close()
	
	def close(self, *args):
		if self.__closevalidate__():
			self.app.hide()
			gtk.main_quit()
		

